package zb.ex;

import java.util.concurrent.TimeUnit;

/**
 * @author wzby
 * @Date: 2022/8/9 13:51
 */
public class Tic2 implements Runnable {


    public static void main(String[] args) {
        Tic2 t = new Tic2(10000);
        new Thread(t::run, "一").start();
        new Thread(t::run, "二").start();
        new Thread(t::run, "三").start();
        new Thread(t::run, "四").start();
        new Thread(t::run, "五").start();
        new Thread(t::run, "六").start();
    }

    private int num;

    public Tic2(int num) {

        this.num = num;
    }


    @Override
    public void run() {
        String s = Thread.currentThread().getName();
        System.out.printf("%s开始%n", s);
        while (true) {
            synchronized (this) {
                if (this.num < 1) {
                    break;
                } else {
                    try {
                        //TimeUnit.SECONDS.sleep(1);
                        System.out.printf("%s售出%d，剩余%d %n", s, num, --num);
                    } catch (Exception e) {

                    }
                }
            }
        }
    }
}
